/**
 * @Author yang
 * @Description //TODO
 * @Date 2020/11/11
 * @Time 9:23
 **/
package main

import (
	"crypto/tls"
	"fmt"
	"go_work_demos/utils/orm/gorm"
	"io/ioutil"
	"math/rand"
	"net/http"
	"net/url"
	"time"
)

type ProxyIP struct {
	ID              string `gorm:"column:id;"`
	Ip              string `gorm:"column:ip;"`
	Port            int    `gorm:"column:port;"`
	Speed           int    `gorm:"column:speed"`
	ConnectionScore int    `gorm:"column:connection_score"`
	AvailbelScore   int    `gorm:"column:availbel_score"`
}

func (this *ProxyIP) TableName() string {
	return "tb_proxy"
}

func (this *ProxyIP) Url() string {
	return fmt.Sprintf("%v:%v", this.Ip, this.Port)
}

func (this *ProxyIP) Select() []ProxyIP {
	var selectAll = []ProxyIP{}
	db := gorm.DB().Table(this.TableName())
	db.Where("connection_score > 0").
		Where("availbel_score > 0").
		Where("speed < 1000").
		Order("speed asc", true).
		Find(&selectAll)
	return selectAll
}

const (
	BAIDU = "http://www.baidu.com"
	//BAIDU = "https://aiqicha.baidu.com/detail/basicAllDataAjax?pid=32682111976511"
	//BAIDU = "http://www.sina.com"
	AIQICHA = "https://aiqicha.baidu.com/detail/basicAllDataAjax?pid=28783255028393"
	//AIQICHA = "https://aiqicha.baidu.com/company_detail_32682111976511"
	//AIQICHA = "https://www.oschina.net/news/120356/exploring-pgo-for-the-rust-compiler"
)

//var proxyUrl = "199.168.148.150:80"
var proxyUrl = "58.220.95.79:10000"

func main() {
	GetAllProxies()
	//TestSingleProxy(proxyUrl)
}

func TestSingleProxy(proxyUrl string) {
	//proxyUrl := "58.218.200.237:13719"
	//ok := TestProxyUrl(proxyUrl, BAIDU)
	//if ok {
	//	fmt.Println("=====> ", proxyUrl)
	//}
	fmt.Println("================================> 爱企查")

	ok1 := TestProxyUrl(proxyUrl, AIQICHA)
	if ok1 {
		fmt.Println("=====> ", proxyUrl)
	}
	return
}

func GetAllProxies() {
	availableProxies := []string{}
	for _, v := range (&ProxyIP{}).Select() {
		proxyUrl := v.Url()
		ok := TestProxyUrl(proxyUrl, BAIDU)
		//ok := TestProxyUrl(proxyUrl, AIQICHA)
		if ok {
			availableProxies = append(availableProxies, proxyUrl)
			fmt.Println("=====> ", proxyUrl)
		}
	}

	for _, v := range availableProxies {
		fmt.Println("=====> ", v)
	}
	return
}

func TestProxyUrl(proxyUrl, webUrl string) (ok bool) {
	//proxyUrl := "http://199.168.148.150:80/"

	//proxyUrl := "http://186.159.5.161:8080/"
	//proxyUrl := "http://127.0.0.1:8080/"
	//webUrl := "https://aiqicha.baidu.com/detail/basicAllDataAjax?pid=28783255028393"
	//webUrl := "http://www.baidu.com"

	proxy, _ := url.Parse(proxyUrl)
	tr := &http.Transport{
		Proxy:           http.ProxyURL(proxy),
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}

	client := &http.Client{
		Transport: tr,
		Timeout:   time.Second * 5, //超时时间
	}

	req, err := http.NewRequest("GET", webUrl, nil)
	req.Header.Set("User-Agent", getAgent())
	//req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
	//req.Header.Set("accept-language", "zh-CN,zh;q=0.9")
	//req.Header.Set("Connection", "keep-alive")

	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("出错了", err)
		return
	}
	if resp.StatusCode == 200 {
		ok = true
	}

	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)
	fmt.Println(string(body))
	return
}

func getAgent() string {
	agent := [...]string{
		"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0",
		"Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11",
		"Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11",
		"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)",
		"Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
		"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; The World)",
		"User-Agent,Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
		"User-Agent, Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)",
		"User-Agent,Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
	}
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	len := len(agent)
	return agent[r.Intn(len)]
}
